/**
 * 
 */
package com.eagle.coders.core.component;

import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

import com.eagle.coders.common.ui.form.renderer.ApplicationRenderer;
import com.eagle.coders.common.ui.interfaces.IComponent;
import com.eagle.coders.core.component.utils.LookupServiceUtils;

/**
 * @author Anees
 *
 */
public class ComponentsLoader {
	
	private static Map<String, IComponent> usecaseComponentMap = new HashMap<String, IComponent>();

	/**
	 * 
	 * @param usecaseID
	 * @return
	 */
	public static IComponent getUsecase(final String usecaseID){
		if(usecaseComponentMap.containsKey(usecaseID)){
			return usecaseComponentMap.get(usecaseID);
		}
		else{
			loadUsecaseComponent();
			return usecaseComponentMap.get(usecaseID);
		}
	}
	
	/**
	 * 
	 * @return
	 */
	public static void loadUsecaseComponent(){
		Collection<IComponent> components = LookupServiceUtils.lookup(IComponent.class);
		for(IComponent comp : components){
			renderUsecase(comp);
			usecaseComponentMap.put(comp.getComponentIdentity(), comp);
		}
	}
	
	/**
	 * 
	 * @param component
	 */
	private static void renderUsecase(final IComponent component){
		ApplicationRenderer applicationRendere = new ApplicationRenderer();
		String usecaseXml = applicationRendere.generateUsecaseForm(component, false);
		component.setXml(usecaseXml);
	}
}
